<?php

namespace App\Http\Controllers;

use App\Models\MyMessage;

use App\Http\Libs\Helper_Huanxin;
use App\Models\Project;
use App\Models\Throughtrain;
use App\Models\User;
use Illuminate\Http\Request;
use \DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Models\ThroughtrainApply;
use App\Models\RoleUser;
use App\Http\Requests\ZtcUseApplyRequest;
class ZtcUseApplyController extends CommonController
{
    /**
     * 获取使用申请列表
     *
     * @return Response
     */
    public function getIndex()
    {
        $lists = ThroughtrainApply::with('user', 'throughtrain.user', 'project', 'maker')->orderBy('created_at', 'desc')->paginate(10);

        return view('ztcuseapply.index')->with('lists', $lists)->with("actions", $this->actions);
    }

    /**
     * 执行一个搜索
     */
    public function search(Request $request)
    {
        $data = $request->input();
        $sql = ThroughtrainApply::with('user', 'throughtrain.user', 'project', 'maker');

        if (isset($data['is_send']) && $data['is_send'] != '') {
            $sql->where('is_send', $data['is_send']);

        }

        if (isset($data['status']) && $data['status'] != '') {
            $sql->where('status', $data['status']);
        }

        if (isset($data['project']) && $data['project'] != '') {
            $pids = Project::where('subject', 'like', '%' . trim($data['project']) . '%')->lists('id');
            $sql->whereIn('project_id', $pids);
        }



        $lists = $sql->paginate(10);
//        dd($lists);

        return view('ztcuseapply.search')->with("lists", $lists)->with("is_send", $data['is_send'])
            ->with("project", $data['project'])->with("status", $data['status'])
            ->with("actions", $this->actions);
    }

    /**
     * 获取编辑页面
     */
    public function getEdit(Request $request)
    {
        $data = $request->input();
        $list = ThroughtrainApply::where('id', $data['id'])->first();

        return view('ztcuseapply.edit')->with('list', $list);
    }

    public function postEdit(ZtcUseApplyRequest $request)
    {
        $data = $request->input();
        ThroughtrainApply::where('id', $data['id'])->update(['address'=>$data['address']]);
        return AjaxCallbackMessage('编辑成功', true, url('ztcuseapply.index'));
    }
}
